package top.chmin.project.cloud.sys.index.column.info;

import java.util.List;
import java.util.ArrayList;
import java.time.LocalDateTime;

import top.chmin.project.cloud.tool.LocalTool;
import top.chmin.project.cloud.common.*;
import lombok.experimental.Accessors;
import lombok.Setter;
import lombok.Getter;



/**
 * chmin
 * 2024-01-26 13:56:02
 * cloud-code
 */
@Getter
@Setter
@Accessors(chain = true)
public class IndexColumnInfo extends Model {
    /*###########auto#start###########*/
    // column编号
    private String columnCode;
    // column顺序
    private Integer columnOrderNo;
    // index编号
    private String indexCode;
    /*###########auto##end############*/
    /* append $modelFieldAppender$ */

    public IndexColumnInfoModelCondition forInsert() {
        return new IndexColumnInfoModelCondition().forInsert();
    }

    public IndexColumnInfoModelCondition forUpdate() {
        return new IndexColumnInfoModelCondition().forUpdate();
    }

    public IndexColumnInfoModelCondition forDelete() {
        return new IndexColumnInfoModelCondition().forDelete();
    }

    public IndexColumnInfoModelCondition forSelect() {
        return new IndexColumnInfoModelCondition().forSelect();
    }

    public class IndexColumnInfoModelCondition extends Condition {
        IndexColumnInfoModelCondition parent = null;

        private IndexColumnInfoModelCondition() {
        }

        private IndexColumnInfoModelCondition(IndexColumnInfoModelCondition condition) {
            this.parent = condition;
        }

        private IndexColumnInfoModelCondition forInsert() {
            IndexColumnInfoModelCondition condition = new IndexColumnInfoModelCondition();
            List<String> columnNameList = new ArrayList<>();
            List<String> valueList = new ArrayList<>();
            /*###########auto#start###########*/
            columnNameList.add("column_code");
            valueList.add("?");
            condition.getValueList().add(getColumnCode());

            columnNameList.add("column_order_no");
            valueList.add("?");
            condition.getValueList().add(getColumnOrderNo());

            columnNameList.add("index_code");
            valueList.add("?");
            condition.getValueList().add(getIndexCode());

            /*###########auto##end############*/
            /* append $modelForInsertFieldAppender$ */
            condition.getValueList().add(LocalTool.getUserId());
            condition.getValueList().add(LocalTool.getUserId());
            condition.getValueList().add(LocalDateTime.now());
            condition.getValueList().add(LocalDateTime.now());
            condition.setSql("INSERT INTO sys_index_column_info(%s, create_by, update_by, create_time, update_time) VALUES (%s, ?, ?, ?, ?)".formatted(
                    String.join(", ", columnNameList),
                    String.join(", ", valueList)
            ));
            return condition;
        }

        private IndexColumnInfoModelCondition forUpdate() {
            IndexColumnInfoModelCondition condition = new IndexColumnInfoModelCondition();
            /*###########auto#start###########*/
            if (columnCode != null) {
                condition.getUpdateList().add("column_code = ?");
                condition.getUpdateValueList().add(columnCode);
            }

            if (columnOrderNo != null) {
                condition.getUpdateList().add("column_order_no = ?");
                condition.getUpdateValueList().add(columnOrderNo);
            }

            if (indexCode != null) {
                condition.getUpdateList().add("index_code = ?");
                condition.getUpdateValueList().add(indexCode);
            }

            /*###########auto##end############*/
            /* append $modelForUpdateFieldAppender$ */
            if (getUpdateBy() != null) {
                condition.getUpdateList().add("update_by = ?");
                condition.getValueList().add(LocalTool.getUserId());
            }
            if (getUpdateTime() != null) {
                condition.getUpdateList().add("update_time = ?");
                condition.getValueList().add(LocalDateTime.now());
            }
            condition.setSql("UPDATE sys_index_column_info#update##where##order##limit#");
            return condition;
        }

        private IndexColumnInfoModelCondition forDelete() {
            IndexColumnInfoModelCondition condition = new IndexColumnInfoModelCondition();
            condition.setSql("DELETE FROM sys_index_column_info#where##group##order##limit#");
            return condition;
        }

        private IndexColumnInfoModelCondition forSelect() {
            IndexColumnInfoModelCondition condition = new IndexColumnInfoModelCondition();
            condition.modelClass = IndexColumnInfo.class;
            condition.setSql("SELECT #select# FROM sys_index_column_info#where##group##order##limit#");
            return condition;
        }

        public IndexColumnInfoModelCondition distinct() {
            this.distinct = true;
            return this;
        }

        public IndexColumnInfoModelCondition limit(int limit) {
            this.limit = " LIMIT " + limit;
            return this;
        }

        public IndexColumnInfoModelCondition sub() {
            return new IndexColumnInfoModelCondition(this);
        }

        public IndexColumnInfoModelCondition finish() {
            if (!whereList.isEmpty()) {
                parent.whereList.add("(" + String.join(" OR ", whereList) + ")");
            }
            parent.whereValueList.addAll(whereValueList);
            return parent;
        }

        public IndexColumnInfoCondition toFlex() {
            IndexColumnInfoCondition flex = new IndexColumnInfoCondition();
            flex.setSql(sql);
            flex.setDistinct(distinct);
            flex.setModelClass(modelClass);
            flex.setSelectList(selectList);
            flex.setUpdateList(updateList);
            flex.setUpdateValueList(updateValueList);
            flex.setWhereList(whereList);
            flex.setWhereValueList(whereValueList);
            flex.setGroupList(groupList);
            flex.setOrderList(orderList);
            flex.setLimit(limit);
            return flex;
        }

        /*###########auto#start###########*/
        public ColumnCode columnCode() {
            return new ColumnCode();
        }

        public class ColumnCode extends StringModelColumn<IndexColumnInfoModelCondition> {
            private ColumnCode() {
                super(IndexColumnInfoModelCondition.this, "column_code", IndexColumnInfo.this.getColumnCode());
            }
        }

        public ColumnOrderNo columnOrderNo() {
            return new ColumnOrderNo();
        }

        public class ColumnOrderNo extends IntegerModelColumn<IndexColumnInfoModelCondition> {
            private ColumnOrderNo() {
                super(IndexColumnInfoModelCondition.this, "column_order_no", IndexColumnInfo.this.getColumnOrderNo());
            }
        }

        public CreateBy createBy() {
            return new CreateBy();
        }

        public class CreateBy extends StringModelColumn<IndexColumnInfoModelCondition> {
            private CreateBy() {
                super(IndexColumnInfoModelCondition.this, "create_by", IndexColumnInfo.this.getCreateBy());
            }
        }

        public CreateTime createTime() {
            return new CreateTime();
        }

        public class CreateTime extends LocalDateTimeModelColumn<IndexColumnInfoModelCondition> {
            private CreateTime() {
                super(IndexColumnInfoModelCondition.this, "create_time", IndexColumnInfo.this.getCreateTime());
            }
        }

        public Id id() {
            return new Id();
        }

        public class Id extends IntegerModelColumn<IndexColumnInfoModelCondition> {
            private Id() {
                super(IndexColumnInfoModelCondition.this, "id", IndexColumnInfo.this.getId());
            }
        }

        public IndexCode indexCode() {
            return new IndexCode();
        }

        public class IndexCode extends StringModelColumn<IndexColumnInfoModelCondition> {
            private IndexCode() {
                super(IndexColumnInfoModelCondition.this, "index_code", IndexColumnInfo.this.getIndexCode());
            }
        }

        public UpdateBy updateBy() {
            return new UpdateBy();
        }

        public class UpdateBy extends StringModelColumn<IndexColumnInfoModelCondition> {
            private UpdateBy() {
                super(IndexColumnInfoModelCondition.this, "update_by", IndexColumnInfo.this.getUpdateBy());
            }
        }

        public UpdateTime updateTime() {
            return new UpdateTime();
        }

        public class UpdateTime extends LocalDateTimeModelColumn<IndexColumnInfoModelCondition> {
            private UpdateTime() {
                super(IndexColumnInfoModelCondition.this, "update_time", IndexColumnInfo.this.getUpdateTime());
            }
        }

        /*###########auto##end############*/
        /* append $modelConditionFieldAppender$ */
    }
}
